﻿@page "/project/{ProjectId}"
@using TaskoMask.Clients.UserPanel.Pages.Projects.Components
@inject ProjectApiService ProjectApiService
@implements IDisposable

@if (Model != null)
{
    <div id="project_page" class="container mx-auto px-md-2">
        <Breadcrumb Model=@Model.Project></Breadcrumb>

        <div class="row">
            <TaskoMask.Clients.UserPanel.Shared.Boards Model=@Model.Boards ProjectId=@Model.Project.Id></TaskoMask.Clients.UserPanel.Shared.Boards>
        </div>
    </div>
}


@code {

    [Parameter]
    public string ProjectId { get; set; }


    public ProjectDetailsViewModel Model { get; set; }



    /// <summary>
    /// /
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        ComponentMessageService.OnMessage += ReloadDataHandler;

        await LoadDataAsync();
    }



    /// <summary>
    /// /
    /// </summary>
    private async Task LoadDataAsync()
    {
        var projectDetailsListResult = await ProjectApiService.GetDetailsAsync(ProjectId);
        if (!projectDetailsListResult.IsSuccess)
            ToastService.ShowError(projectDetailsListResult.Errors.ParseToFragment(), projectDetailsListResult.Message);
        else
            Model = projectDetailsListResult.Value;
    }



    /// <summary>
    ///This is a handler to be notified when some interesting events happened
    /// </summary>
    private async void ReloadDataHandler(MessageType messageType)
    {
        try
        {
            if (ReloadDataHelper.Project_Index_Need_Reload(messageType))
            {
                await LoadDataAsync();
                StateHasChanged();
            }
        }
        catch
        {
            //Async task failure because of async void challenges
        }
    }



    /// <summary>
    ///
    /// </summary>
    public void Dispose()
    {
        // unsubscribe from OnMessage event
        ComponentMessageService.OnMessage -= ReloadDataHandler;
    }



}
